//
//  WmcAccountCancellationVC.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/6/20.
//

import UIKit
import WebKit
import YPBedrock

// MARK: - 扩展WmcAccountCancellationVC + WmcNavigatorProtocol

extension WmcAccountCancellationVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.cancellationAccount]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        let vc = WmcAccountCancellationVC()
        return vc
    }
}

// MARK: - [WmcAccountCancellationVC]

//
//// MARK: - 扩展WmcAccountCancellationVC + WmcVMProtocol
//
// extension WmcAccountCancellationVC: WmcVMProtocol {
//    typealias VMType = WmcAccountCancellationVM
// }

class WmcAccountCancellationVC: WmcBaseVC {
    // Lifecycle

    @available(*, unavailable)
    required convenience init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    lazy var config: WKWebViewConfiguration = {
        let config = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        config.userContentController = userContentController

        let preferences = WKPreferences()
        preferences.minimumFontSize = 12
        preferences.javaScriptCanOpenWindowsAutomatically = true
        config.preferences = preferences

        return config
    }()

    lazy var webView: WKWebView = {
        let view = WKWebView(frame: .zero, configuration: config)
        view.uiDelegate = self
        view.backgroundColor = .white
        view.isHidden = true
        view.navigationDelegate = self
        view.scrollView.bounces = true
        view.scrollView.showsVerticalScrollIndicator = false
        return view
    }()

    var urlStr: String? {
        #if DEBUG
            return "http://m.zhaogong.vrtbbs.com/h5/cameralogout/explain/"
        #else
            return "http://m.yupao.com/h5/cameralogout/explain/"
        #endif
    }

    override func makeUI() {
        title = "注销账号"
        makeNavView(showShadow: false)
        addSubview(webView)
        isUserAgree = false
        layoutUI()
        loadData()

        WmcLogManager.Log(NSHomeDirectory())
    }

    // Private

    private let handleUrl = "yupao://cancllation"

    private var isUserAgree = false {
        didSet {}
    }
}

// MARK: - WKNavigationDelegate, WKUIDelegate

extension WmcAccountCancellationVC: WKNavigationDelegate, WKUIDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let uStr = navigationAction.request.url?.absoluteString, (uStr.contains("chack-agreement") || uStr.contains("sumbit-logout")) {
            if !uStr.contains("sumbit-logout") {
                /// 跳转注销政策
                #if DEBUG
                    WmcNavigatorManager.push(.cancellationPolicy, parms: ["url": "http://m.zhaogong.vrtbbs.com/logout/agreement/"])
                #else
                    WmcNavigatorManager.push(.cancellationPolicy, parms: ["url": "http://m.yupao.com/h5/cameralogout/agreement/"])
                #endif
            } else {
                WmcNavigatorManager.push(.cancellationFeedback)
            }
            decisionHandler(.cancel)
            return
        }
        decisionHandler(.allow)
        return
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.body.style.backgroundColor=\"#ffffff\"") { [weak self] _, e in
            guard let weakSelf = self else { return }
            weakSelf.showWebView()
            WmcHudManager.dismiss()
        }
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        // YPProgressHUD.show(toast: error.localizedDescription)
    }
}

// MARK: - 拼接本地html

private extension WmcAccountCancellationVC {
    func loadData() {
        if let uStr = urlStr, let url = URL(string: uStr) {
            WmcHudManager.loading()
            webView.load(URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData))
        }
    }

    func layoutUI() {
        webView.snp.remakeConstraints { make in
            make.top.equalTo(self.navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    func showWebView() {
        webView.isHidden = false
    }
}
